package demo2
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Matrix;

	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.phys.BodyType;
	import nape.shape.Polygon;
	import nape.space.Space;

	public final class ImageSkinBox
	{
		[Embed(source="blackBox.png")]
		private static var _boxClass:Class;

		private var boxContainer:DisplayObject;
		private var boxBody:Body;

		//需要先记录没有旋转时候的Width和Height
		//因为一旦旋转后实时取得的Width和Height有错误
		private var width:Number;
		private var height:Number;

		private var matrix:Matrix;

		public function ImageSkinBox()
		{
		}

		public function initialize(container:DisplayObjectContainer, _posX:Number, _posY:Number, _space:Space):void
		{
			boxContainer=new _boxClass();
			container.addChild(boxContainer);
			boxContainer.x=_posX;
			boxContainer.y=_posY;
			width=boxContainer.width;
			height=boxContainer.height;

			boxBody=new Body(BodyType.DYNAMIC, new Vec2(_posX, _posY));

			var shape:Polygon=new Polygon(Polygon.box(boxContainer.width, boxContainer.height));
			boxBody.shapes.add(shape);
			boxBody.space=_space;

			boxBody.userData["target"]=this;

			matrix=new Matrix();

		}

		public function adjustDisplay():void
		{
			matrix.identity();
			matrix.translate(-width / 2, -height / 2);
			matrix.rotate(boxBody.rotation)
			matrix.translate(boxBody.position.x, boxBody.position.y);
			boxContainer.transform.matrix=matrix;
		}

	}
}
